package cn.my.springHDFS.service;

import cn.my.springHDFS.domain.AdminUser;
import cn.my.springHDFS.domain.User;
import cn.my.springHDFS.domain.UserLoginHistory;
import cn.my.springHDFS.repository.UserHistoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Leo on 2017/7/7.
 * 用户登录记录服务层
 */
@SuppressWarnings("Duplicates")
@Repository
public class UserHistoryServiceImpl {

    @Autowired
    private UserHistoryRepository userHistoryRepository;

    private String account;

    private String accountLevel;

    /**
     * 记录用户登陆日志
     * @param objClass
     * @param httpServletRequest
     * @return
     */
    public boolean addUserLoginLogger(Object objClass, HttpServletRequest httpServletRequest){
        if (objClass.getClass().equals(User.class)){
            User user = (User) objClass;
            account = user.getAccount();
            accountLevel = user.getUserLevel();
        }
        else if (objClass.getClass().equals(AdminUser.class)){
            AdminUser adminUser = (AdminUser) objClass;
            account = adminUser.getAdminAccount();
            accountLevel = adminUser.getAdminLevel();
        }
        else {
            return false;
        }

        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //封装用户登录日志实体类
        UserLoginHistory uLoginHis = new UserLoginHistory();
        uLoginHis.setUserAccount(account);
        uLoginHis.setLoginTime(df.format(new Date()));
        uLoginHis.setLogoutTime("");
        uLoginHis.setLoginAddress(httpServletRequest.getRemoteAddr());
        uLoginHis.setUserAccountLevel(accountLevel);

        //写入数据库
        try {
            userHistoryRepository.insert(uLoginHis);
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 记录用户登出日志
     * @param objClass
     * @param httpServletRequest
     * @return
     */
    public boolean addUserLogoutLogger(Object objClass, HttpServletRequest httpServletRequest){
        if (objClass.getClass().equals(User.class)){
            User user = (User) objClass;
            account = user.getAccount();
            accountLevel = user.getUserLevel();
        }
        else if (objClass.getClass().equals(AdminUser.class)){
            AdminUser adminUser = (AdminUser) objClass;
            account = adminUser.getAdminAccount();
            accountLevel = adminUser.getAdminLevel();
        }
        else {
            return false;
        }

        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //封装用户登录日志实体类
        UserLoginHistory uLogoutHis = new UserLoginHistory();
        uLogoutHis.setUserAccount(account);
        uLogoutHis.setLoginTime("");
        uLogoutHis.setLogoutTime(df.format(new Date()));
        uLogoutHis.setLoginAddress(httpServletRequest.getRemoteAddr());
        uLogoutHis.setUserAccountLevel(accountLevel);

        //写入数据库
        try {
            userHistoryRepository.insert(uLogoutHis);
            return true;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
